<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadController extends BaseController
{
    /**
     * 多图上传
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadSomeImage(Request $request)
    {
        $pics = $request->file('image');

        $success = [];  // 成功的图片index
        $fail = []; // 失败的图片index
        $image = []; // 返回的图片list
        foreach ($pics as $index => $pic) {
            if (!$pic) {
                $fail[] = $index;
                break;
            }
            // $name = $pic->getClientOriginalName();//得到图片名；
            $ext = strtolower($pic->getClientOriginalExtension());//得到图片后缀；
            if (!in_array($ext, ['jpg', 'jpeg', 'png', 'bmp'])) {
                $fail[] = $index;
                break;
                // return $this->jsonResultFail('仅支持 jpg jpeg png bmp文件');
            }
            // uniqid()
            $fileName = md5(microtime());
            $fileName = date('Y/m/d') . '/' . $fileName . '.' . $ext;//生成新的的文件名
            $bool = Storage::disk('cosv5')->put($fileName, file_get_contents($pic->getRealPath()));//
            $domain = env('COSV5_WANXIANG_URL');
            // $data['pic'] = env('COSV5_WANXIANG_URL') . '/' . $fileName;//返回文件路径存贮在数据库
            if (!$bool) {
                $fail[] = $index;
            } else {
                $success[] = $index;
                $image[] = $fileName;
            }
        }

        return $this->jsonResultOk(compact('domain', 'image', 'fail'));
    }
}
